package main

import (
	"github.com/spf13/viper"
	"log"
	"time"
)

type Config struct {
	Alb         Alb         `yaml:"alb" mapstructure:"alb"`
	App         App         `yaml:"app" mapstructure:"app"`
	HealthCheck HealthCheck `yaml:"health_check" mapstructure:"healthCheck"`
	WeightCheck WeightCheck `yaml:"weight_Check" mapstructure:"weightCheck"`
	Back        Back        `yaml:"back" mapstructure:"back"`
	Unzip       Unzip       `yaml:"unzip" mapstructure:"unzip"`
}

type Alb struct {
	AccessKeyId     string `yaml:"accessKeyId" mapstructure:"accessKeyId"`
	AccessKeySecret string `yaml:"accessKeySecret" mapstructure:"accessKeySecret"`
	ServerGroupId   string `yaml:"serverGroupId" mapstructure:"serverGroupId"`
	Endpoint        string `yaml:"endpoint" mapstructure:"endpoint"`
}

type App struct {
	Ip      string `yaml:"ip" mapstructure:"ip"`
	AppName string `yaml:"appName" mapstructure:"appName"`
	AppDir  string `yaml:"appDir" mapstructure:"appDir"`
	AppCmd  string `yaml:"appCmd" mapstructure:"appCmd"`
}

type HealthCheck struct {
	Url                 string        `yaml:"url" mapstructure:"url"`
	CheckNum            int           `yaml:"checkNum" mapstructure:"checkNum"`
	CheckTimeout        time.Duration `yaml:"checkTimeout" mapstructure:"checkTimeout"`
	CheckProcessTimeout time.Duration `yaml:"checkProcessTimeout" mapstructure:"checkProcessTimeout"`
}

type WeightCheck struct {
	CheckNum            int           `yaml:"checkNum" mapstructure:"checkNum"`
	CheckTimeout        time.Duration `yaml:"checkTimeout" mapstructure:"checkTimeout"`
	CheckProcessTimeout time.Duration `yaml:"checkProcessTimeout" mapstructure:"checkProcessTimeout"`
}

type Back struct {
	Enabled bool   `yaml:"enabled" mapstructure:"enabled"`
	BackDir string `yaml:"backDir" mapstructure:"backDir"`
	BackCmd string `yaml:"backCmd" mapstructure:"backCmd"`
}

type Unzip struct {
	Enabled  bool   `yaml:"enabled" mapstructure:"enabled"`
	UnzipCmd string `yaml:"unzipCmd" mapstructure:"unzipCmd"`
	UnzipDir string `yaml:"unzipDir" mapstructure:"unzipDir"`
}

var AppConfig Config

func LoadConfig() {
	viper.SetConfigName("config") // 配置文件名（不带扩展名）
	viper.SetConfigType("yaml")   // 指定配置文件类型
	viper.AddConfigPath(".")      // 添加配置文件路径（当前目录）
	// 2. 读取配置文件
	err := viper.ReadInConfig()
	if err != nil {
		log.Fatalf("Error reading config file: %v\n", err)
	}

	err = viper.Unmarshal(&AppConfig)
	if err != nil {
		log.Fatalf("Error unmarshaling config into struct: %v\n", err)
	}
}
